﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using GrainManage.Data.Util;
using GrainManage.Data.Model.Filter;
using GrainManage.Data.Model.Table;
using GrainManage.Data.Model;

namespace GrainManage.Data.Service
{
    public class PriceService : BaseService
    {

        public PriceService(GrainManage.Data.Access.AccessBase proc, GrainManage.Data.Pattern.IPattern pattern) : base(proc, pattern) { }

        public List<string> GetGrainType(IPageable page)
        {
            return GetTypeName(TypeFlag.Grain, page);
        }

        public List<string> GetPriceType(IPageable page)
        {
            return GetTypeName(TypeFlag.Default, page);
        }

        public List<PriceTable> GetRecentPrice(PriceFilter filter, IPageable page, string creator)
        {
            Pattern.SetModel(filter, GrainManage.Data.Pattern.PatternMode.Original);
            DataSet grain = Proc.ExecuteQuery(ProcConst.Price_GetRecentByGrainExtent, filter.PriceType, filter.Grain, GetOffset(page), page.PageSize, creator);
            return ConvertToModel<PriceTable>(grain, null);
        }

        public List<PriceTable> GetPrice(PriceFilter filter, IPageable page, ICountable records, string creator)
        {
            Pattern.SetModel(filter, GrainManage.Data.Pattern.PatternMode.Original);
            DataSet grain = Proc.ExecuteQuery(ProcConst.Price_GetAllByGrainTimeExtent, filter.PriceType, filter.Grain,
                filter.StartTime, filter.EndTime, GetOffset(page), page.PageSize, creator);
            return ConvertToModel<PriceTable>(grain, records);
        }

        public int Insert(PriceTable price)
        {
            Initializor.Initialize(price, string.Empty);
            var result = Proc.ExecuteScalar(ProcConst.Price_Insert, price.Grain, price.UnitPrice, price.PriceType, price.Remarks, price.Creator);
            return result == null ? 0 : Convert.ToInt32(result);
        }

        public int Update(PriceTable price)
        {
            Initializor.Initialize(price, string.Empty);
            return Proc.Execute(ProcConst.Price_UpdateByID, price.PriceID, price.Grain, price.UnitPrice, price.PriceType, price.Remarks, price.Creator);
        }

        public int Delete(int priceID)
        {
            return Proc.Execute(ProcConst.Price_DeleteByID, priceID);
        }

        public List<int> Insert(List<PriceTable> prices)
        {
            var priceIDs = new List<int>();
            foreach (var price in prices)
            {
                priceIDs.Add(Insert(price));
            }
            return priceIDs;
        }

        public List<int> Update(List<PriceTable> prices)
        {
            var invalidPrices = new List<int>();
            foreach (var price in prices)
            {
                if (Update(price) == 0)
                {
                    invalidPrices.Add(price.PriceID);
                }
            }
            return invalidPrices;
        }

        public List<int> Delete(List<int> prices)
        {
            var invalidPrices = new List<int>();
            foreach (var price in prices)
            {
                if (Delete(price) == 0)
                {
                    invalidPrices.Add(price);
                }
            }
            return invalidPrices;
        }

        #region 辅助函数

        private List<string> GetTypeName(TypeFlag typeFlag, IPageable page)
        {
            var service = new CommonService(Proc);
            var fieldList = service.GetTypeName(typeFlag, page);
            return fieldList;
        }

        #endregion
    }
}
